package com.yoofn.framework.function

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.support.annotation.AnimRes
import android.support.v4.app.Fragment
import com.yoofn.framework.R
import com.yoofn.framework.activity.BaseActivity
import com.yoofn.framework.activity.FragmentReplaceActivity
import com.yoofn.framework.activity.WebActivity
import com.yoofn.framework.fragment.BaseFragment
import java.lang.ref.WeakReference

/**
 * Created by kalshen on 2017/9/11.
 */
class PageFunction {


    constructor(activity: WeakReference<BaseActivity<*>>) {}
    constructor(fragment: WeakReference<BaseFragment<*>>)

    /**
     * 开启一个webActivity
     */
    fun startWebActivity(url: String) {
        startWebActivity(url)
    }

    /**
     * 开启一个webActivity
     * @param url webView的url地址
     */
    fun startWebActivity(url: String, @AnimRes enterAnim: Int? = null, @AnimRes exitAnim: Int? = null) {
        val intent = Intent(activity, WebActivity::class.java)
        intent.putExtra(WebActivity.WEB_URL, url)
        activity.startActivity(intent)
        activity.overridePendingTransition(enterAnim ?: R.anim.in_from_right, exitAnim ?: R.anim.out_to_left)
    }

    /**
     * 开启一个replaceActivity
     * @param args 传递给fragment 的argument
     * @param enterAnim 进入动画（可传） 默认为右进
     * @param exitAnim 退出动画（可传） 默认为左出
     */
    fun startFragment(args: Bundle, @AnimRes enterAnim: Int? = null, @AnimRes exitAnim: Int? = null) {
        startFragmentForResult(args, enterAnim, exitAnim)
    }

    /**
     * 开启一个带requestCode 的replaceActivity
     * @param args 传递给fragment 的argument
     * @param enterAnim 进入动画（可传） 默认为右进
     * @param exitAnim 退出动画（可传） 默认为左出
     * @param requestCode
     */
    fun startFragmentForResult(args: Bundle, @AnimRes enterAnim: Int? = null, @AnimRes exitAnim: Int? = null, requestCode: Int? = null) {
        val intent = Intent(activity, FragmentReplaceActivity::class.java)
        intent.putExtra(FragmentReplaceActivity.BUNDLE, args)
        if (requestCode == null) {
            activity.startActivity(intent)
        } else {
            activity.startActivityForResult(intent, requestCode)
        }
        activity.overridePendingTransition(enterAnim ?: R.anim.in_from_right, exitAnim ?: R.anim.out_to_left)
    }
}